using System.Text.Json;
using System.Text.Json.Nodes;
using Engine.Graphics;
using Game;
using SCIENEW.Tech;

namespace SCIENEW {
    public class IndustrialModLoader : ModLoader {
        public static ModEntity ModEntity { get; set; }


        /// <summary>
        /// 原版方块材质
        /// </summary>
        public static Texture2D BaseTexture => BlocksTexturesManager.DefaultBlocksTexture ?? ContentManager.Get<Texture2D>("Textures/Blocks");

        /// <summary>
        /// 工业mod独家材质
        /// </summary>
        public static Texture2D IETexture {
            get {
                if (m_ieTexture == null) m_ieTexture = ContentManager.Get<Texture2D>("IndustrialMod");
                return m_ieTexture;
            }
        }
        static Texture2D m_ieTexture;

        public static string ModPackageName => "com.IndustrialAgeNew";

        public override void __ModInitialize() {
            ModEntity = Entity;
            ModsManager.RegisterHook("OnLoadingStart", this);
            ModsManager.RegisterHook("OnLoadingFinished", this);
            ModsManager.RegisterHook("SubsystemUpdate", this);
        }

        public override void OnLoadingStart(List<Action> actions) {
            ItemsBlockManager.Init();
            NormalBlockManager.Init();
            DevicesBlockManager.Init();
            DamageableItemManager.Init();

            FluidsManager.Init();
        }

        public override void OnLoadingFinished(List<Action> actions) {
            base.OnLoadingFinished(actions);
            m_ieTexture = ContentManager.Get<Texture2D>("IndustrialMod");
            IECraftingRecipesManager.Init();

            ItemsBlockManager.LateInit();
            NormalBlockManager.LateInit();
            DevicesBlockManager.LateInit();

            IECrafterManager.Init();
            TechTreeManager.Init();

            #if false
            string jsonContent = ContentManager.Get<string>("Lang/zh-CN");
            JsonNode jsonNode = JsonNode.Parse(jsonContent);
            JsonNode targetNode = jsonNode["Items"];

            JsonObject nameNode = new();

            foreach (var device in ItemsBlockManager.Items) {
                nameNode.Add(device.GetCraftingId(), device.GetDisplayName(null, 0));
            }
            targetNode["Names"] = nameNode;

            JsonObject descNode = new();

            foreach (var device in ItemsBlockManager.Items) {
                descNode.Add(device.GetCraftingId(), device.GetDescription(0));
            }
            targetNode["Descriptions"] = descNode;

            // 保存修改后的 JSON 到文件
            string jsonFilePath = @"D:\zh-CN.json";
            // 将修改后的 JSON 数据保存为 UTF-8 编码
            using (FileStream fs = new FileStream(jsonFilePath, FileMode.Create, FileAccess.Write))
            using (Utf8JsonWriter writer = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true, Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping }))
            {
                jsonNode.WriteTo(writer);
            }
#endif
        }

        public override void SubsystemUpdate(float dt) {
            base.SubsystemUpdate(dt);
            TechTreeManager.Update();
        }

        public static void GetFile(string filename, Action<Stream> streamAction) => ModsManager.ModList.First(entity => entity.modInfo.PackageName == ModPackageName).GetFile(filename, streamAction);
    }
}